﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TruckScale.Helpers;
using static TruckScale.Helpers.LogHelper;

namespace TruckScale.Models
{
  public class WeighFormModel
  {
    public int? AutoNo { get; set; }
    public string WeighFormKey { get; set; }
    public string WeighFormValue { get; set; }
    public bool IsActive { get; set; }
    public int? SortNo { get; set; }

    public static List<WeighFormModel> SelectAllField()
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = @"select * from weighform order by sort_no;";
          IEnumerable<WeighFormModel> output = cnn.Query<WeighFormModel>(sql);
          return output.ToList();
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return null;
        }
      }
    }
    public static List<WeighFormModel> SelectActiveField()
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = @"select * from weighform where is_active = 1 order by sort_no;";
          IEnumerable<WeighFormModel> output = cnn.Query<WeighFormModel>(sql);
          return output.ToList();
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return null;
        }
      }
    }

    public static void UpdateAllField(List<WeighFormModel> wList)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = @"update weighform set weighform_value=@WeighFormValue, is_active=@IsActive, sort_no=@SortNo where auto_no=@AutoNo";
          cnn.Execute(sql, wList);
          return;
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return;
        }
      }
    }
  }
}
